{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Loops\n",
    "\n",
    "Topics:\n",
    "1. `while` loops\n",
    "2. `for` loops\n",
    "<br>\n",
    "\n",
    "## while loops\n",
    "\n",
    "The syntax for a `while` is\n",
    "\n",
    "```julia\n",
    "while *condition*\n",
    "    *loop body*\n",
    "end\n",
    "```\n",
    "\n",
    "For example, we could use `while` to count or to iterate over an array."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 0\n",
    "while n < 10\n",
    "    n += 1\n",
    "    println(n)\n",
    "end\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "myfriends = [\"Ted\", \"Robyn\", \"Barney\", \"Lily\", \"Marshall\"]\n",
    "\n",
    "i = 1\n",
    "while i <= length(myfriends)\n",
    "    friend = myfriends[i]\n",
    "    println(\"Hi $friend, it's great to see you!\")\n",
    "    i += 1\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## for loops\n",
    "\n",
    "The syntax for a `for` loop is\n",
    "\n",
    "```julia\n",
    "for *var* in *loop iterable*\n",
    "    *loop body*\n",
    "end\n",
    "```\n",
    "\n",
    "We could use a for loop to generate the same results as either of the examples above:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for n in 1:10\n",
    "    println(n)\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "myfriends = [\"Ted\", \"Robyn\", \"Barney\", \"Lily\", \"Marshall\"]\n",
    "\n",
    "for friend in myfriends\n",
    "    println(\"Hi $friend, it's great to see you!\")\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's use `for` loops to create some addition tables, where the value of every entry is the sum of its row and column indices. <br>\n",
    "\n",
    "First, we initialize an array with zeros."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m, n = 5, 5\n",
    "A = fill(0, (m, n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in 1:m\n",
    "    for j in 1:n\n",
    "        A[i, j] = i + j\n",
    "    end\n",
    "end\n",
    "A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's some syntactic sugar for the same nested `for` loop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "B = fill(0, (m, n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in 1:m, j in 1:n\n",
    "    B[i, j] = i + j\n",
    "end\n",
    "B"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The more \"Julia\" way to create this addition table would have been with an *array comprehension*."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "C = [i + j for i in 1:m, j in 1:n]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercises\n",
    "\n",
    "#### 4.1 \n",
    "Loop over integers between 1 and 100 and print their squares."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.2 \n",
    "Add to the code above a bit to create a dictionary, `squares` that holds integers and their squares as key, value pairs such that\n",
    "\n",
    "```julia\n",
    "squares[10] == 100\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "deletable": false,
    "editable": false,
    "hide_input": true,
    "nbgrader": {
     "checksum": "3e96c8051261bb7e0da6df69d54fbe89",
     "grade": true,
     "grade_id": "cell-54834a2e4e122b70",
     "locked": true,
     "points": 1,
     "schema_version": 1,
     "solution": false
    }
   },
   "outputs": [],
   "source": [
    "@assert squares[10] == 100\n",
    "@assert squares[11] == 121"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "#### 4.3 \n",
    "Use an array comprehension to create an an array `squares_arr` that stores the squares for all integers between 1 and 100."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "deletable": false,
    "editable": false,
    "hide_input": true,
    "nbgrader": {
     "checksum": "f0331aaa26a8ec9bc2e637073115083c",
     "grade": true,
     "grade_id": "cell-17d4631c51b8b4f7",
     "locked": true,
     "points": 1,
     "schema_version": 1,
     "solution": false
    }
   },
   "outputs": [],
   "source": [
    "@assert length(squares_arr) == 100\n",
    "@assert sum(squares_arr) == 338350"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Please click on `Validate` on the top, once you are done with the exercises."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.0.0",
   "language": "julia",
   "name": "julia-1.0"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.0.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
